#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    typedef pair<int, int> PII;
    int findLHS(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        int ans = 0, n = nums.size();
        int left = 0, right = 0;
        int pre = 0, pre_size = 0;
        // 先找到所有第一个数的数量
        while (right < n && nums[right] == nums[left])
            ++right;
        pre_size = right - left, pre = nums[left];
        left = right;
        while (right < n)
        {
            // 先找到当前所有相同的数
            while (right < n && nums[right] == nums[left])
                ++right;
            int sz = right - left;
            if (nums[left] == pre + 1)
                ans = max(ans, sz + pre_size);
            pre = nums[left], pre_size = sz;
            left = right;
        }
        return ans;
    }
};